package com.yanqu.road.server.cmd.activity.hell;


import com.yanqu.road.entity.activity.hell.HellNpc;
import com.yanqu.road.pb.activity.HellProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.hell.HellModule;
import com.yanqu.road.server.manger.activity.hell.HellMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_HELL_NPC_NOTE, desc = "断案")
public class HellNpcNoteCmd extends AbstractCommand {

    int activityId;

    Map<Integer, Map<Integer, HellNpc>> result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HellProto.HellNpcNoteReqMsg reqMsg = HellProto.HellNpcNoteReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(HellMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!HellMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        result = player.getModule(HellModule.class).getNpcNote();
    }

    @Override
    public void sendData(GamePlayer player) {
        if(result != null){
            HellProto.HellNpcNoteRespMsg.Builder respMsg = HellProto.HellNpcNoteRespMsg.newBuilder();
            for(int qualityId : result.keySet()){
                HellProto.HellNoteDataTemp.Builder noteDataTempMsg = HellProto.HellNoteDataTemp.newBuilder();
                noteDataTempMsg.setQualityId(qualityId);
                Map<Integer, HellNpc> npcMap = result.get(qualityId);
                for(HellNpc npc : npcMap.values()){
                    HellProto.NpcTemp.Builder npcTempMsg = HellProto.NpcTemp.newBuilder();
                    npcTempMsg.setNpcId(npc.getNpcId());
                    npcTempMsg.setNo(npc.getNo());
                    npcTempMsg.setQualityId(npc.getQualityId());
                    npcTempMsg.setAgeId(npc.getAgeId());
                    for(List<String> eventInfoList : npc.getEventMap().values()){
                        for(String eventInfoStr : eventInfoList){
                            List<Integer> tempList = StringUtils.stringToIntegerList(eventInfoStr,"=");
                            HellProto.EventTemp.Builder eventTempMsg = HellProto.EventTemp.newBuilder();
                            eventTempMsg.setEventId(tempList.get(0));
                            eventTempMsg.setAge(tempList.get(1));
                            npcTempMsg.addEventList(eventTempMsg);
                        }
                    }
                    noteDataTempMsg.addNpcList(npcTempMsg);
                }
                respMsg.addNoteList(noteDataTempMsg);
            }
            player.sendPacket(Protocol.U_HELL_NPC_NOTE, respMsg);
        }
    }
}
